import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';
export function assembleAuthUrl (hosturl, apiKey, apiSecret) {
  const urlObj = new URL(hosturl);
  let date = new Date().toUTCString(); // 签名时间
  // 参与签名的字段 host, date, request-line
  let signString = [
    `host: ${urlObj.host}`,
    `date: ${date}`,
    `GET ${urlObj.pathname} HTTP/1.1`
  ].join('\n');

  // 签名结果
  let signature = Base64.stringify(hmacSHA512(sha256(signString), apiSecret));// 假设你有一个函数来生成签名

  // 构建请求参数
  let authParams = `api_key="${apiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${signature}"`;
  let encodedAuthParams = btoa(authParams)

  // 将编码后的字符串添加到URL后面
  let callUrl = `${hosturl}?authorization=${encodeURIComponent(encodedAuthParams)}`;
  return callUrl;
}